package org.adl.registry.types;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletRequest;

/**
 * 
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public class RimRequest
{
    private ServletRequest mRequest;
    private ArrayList<RimParameter> mRequestParameters;
    
    /**
     *
     */
    public RimRequest(ServletRequest iRequest)
    throws IOException
    {
        this.mRequestParameters = new ArrayList<RimParameter>();
        
        this.mRequest = iRequest;
        
        String encoding = iRequest.getContentType();
        
        if (encoding.equalsIgnoreCase("multipart/form-data"))
        {
            this.parseMultipartRequest();
        }
        else
        {
            this.parseUrlEncodedRequest();
        }
    }
    
    /**
     *
     */
    public void addParameter(RimParameter iParameter)
    {
        this.mRequestParameters.add(iParameter);
    }
    
    /**
     *
     */
    public boolean containsParameter(RimParameter iParameter)
    {
        boolean contains = false;
        
        Iterator<RimParameter> iterator = this.mRequestParameters.iterator();
        
        while (iterator.hasNext())
        {
            RimParameter parameter = iterator.next();
            
            if (iParameter.getClass().isAssignableFrom(parameter.getClass()))
            {
                contains = true;
                
                break;
            }
        }
        
        return contains;
    }
    
    /**
     *
     */
    public boolean isValid()
    {
        boolean valid = false;
        
        return valid;
    }
    
    /**
     *
     */
    public String getRequestAsQueryString()
    {
        StringBuffer queryString = new StringBuffer();
        
        Iterator<RimParameter> iterator = this.mRequestParameters.iterator();
        
        while (iterator.hasNext())
        {
            RimParameter parameter = iterator.next();
            
            String parameterName = parameter.getParameterName();
            String parameterValue = parameter.getParameterValue();
            
            queryString.append(parameterName + "=" + parameterValue);
            
            if (iterator.hasNext())
            {
                queryString.append("&");
            }
        }
        
        return queryString.toString();
    }
    
    /**
     *
     */
    public String getRequestAsFormattedURL(String iRimUrl)
    {
        if (iRimUrl.endsWith("?") == false)
        {
            iRimUrl += "?";
        }
        
        String url = iRimUrl + this.getRequestAsQueryString();
        
        return url;
    }
    
    /**
     *
     */
    private void parseUrlEncodedRequest()
    {
        Map map = this.mRequest.getParameterMap();
        
        Set keys = map.keySet();
        
        Iterator iterator = keys.iterator();
        
        while (iterator.hasNext())
        {
            String key = (String)iterator.next();
            String val = (String)map.get(key);
            
            this.buildTypedParameter(key, val);
        }
    }
    
    /**
     *
     *
     * http://www.netalive.org/topics/8748
     */
    private void parseMultipartRequest()
    throws IOException
    {
        MultipartFormData multipart = new MultipartFormData(this.mRequest);
        
        
        Hashtable parameters = multipart.getParameters();
        
        Set keys = parameters.keySet();
        
        Iterator iterator = keys.iterator();
        
        while (iterator.hasNext())
        {
            String key = (String)iterator.next();
            String val = (String)parameters.get(key);
            
            this.buildTypedParameter(key, val);
        }
    }
    
    /**
     *
     */
    private void buildTypedParameter(String iKey, String iValue)
    {
        
    }
    
}
